﻿namespace Dcms.Common
{
    using Dcms.BLL;
    using Dcms.Model;
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Web;
    using System.Web.UI;

    public class ImageUploadUtil : Page
    {
        private Dcms_ImagesBLL imgBll = new Dcms_ImagesBLL();

        private void CheckUpDirectory(string uploadDirectory)
        {
            try
            {
                if (!Directory.Exists(uploadDirectory)) Directory.CreateDirectory(uploadDirectory);
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError("Dcms.Common.ImageUploadUtil.CheckUpDirectory", ex);
                throw ex;
            }
        }

        public void DeleteImage(Dcms_ImagesModel img)
        {
            try
            {
                this.imgBll.DeleteModelByID(img.Img_ID);
                if (img.Img_Path != null) File.Delete(base.Server.MapPath(img.Img_Path));
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError("Dcms.Common.ImageUploadUtil.DeleteImage", ex);
                throw ex;
            }
        }

        public Dcms_ImagesModel GetImagesByID(int id)
        {
            Dcms_ImagesModel img = new Dcms_ImagesModel();
            return this.imgBll.GetModelByID(id);
        }

        public Dcms_ImagesModel ImageUpload(string path, string filName, int Type)
        {
            Dcms_ImagesModel csTemp0000;
            try
            {
                Dcms_ImagesModel img = new Dcms_ImagesModel();
                string Pakeage = DateTime.Now.ToString("yyyy-MM-dd");
                this.CheckUpDirectory(base.Server.MapPath(path + "/" + DateTime.Now.ToString("yyyy-MM-dd")));
                HttpPostedFile file = HttpContext.Current.Request.Files[filName];
                string FileName = Path.GetFileName(file.FileName);
                string ExtensionName = Path.GetExtension(file.FileName);
                if (ExtensionName != string.Empty)
                {
                    FileName = Guid.NewGuid().ToString();
                    file.SaveAs(base.Server.MapPath(path + "/" + Pakeage + "/" + FileName + ExtensionName));
                    img.Img_Name = Pakeage + "/" + FileName + ExtensionName;
                    img.Img_Path = path + "/" + Pakeage + "/" + FileName + ExtensionName;
                    img.Img_Type = Type;
                    img.Img_Date = DateTime.Now;
                    return this.imgBll.AddModel(img);
                }
                img.Img_ID = 0;
                img.Img_Path = "";
                csTemp0000 = img;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError("Dcms.Common.ImageUploadUtil.ImageUpload3", ex);
                throw ex;
            }
            return csTemp0000;
        }

        public Dcms_ImagesModel ImageUpload(string SPath, int Width, int Height, string UploadName, int Type)
        {
            Dcms_ImagesModel csTemp0000= null;
            try
            {
                Dcms_ImagesModel img = new Dcms_ImagesModel();
                string package = DateTime.Now.ToString("yyyy-MM-dd");
                this.CheckUpDirectory(base.Server.MapPath("Temp"));
                HttpPostedFile file = HttpContext.Current.Request.Files[UploadName];
                string ExtensionName = Path.GetExtension(file.FileName);
                if (ExtensionName != string.Empty)
                {
                    string FileName = Guid.NewGuid().ToString();
                    file.SaveAs(base.Server.MapPath("Temp/" + FileName + ExtensionName));
                    Image oldImage = Image.FromFile(base.Server.MapPath("Temp/" + FileName + ExtensionName));
                    Bitmap newImage = new Bitmap(Width, Height);
                    newImage.SetResolution(oldImage.HorizontalResolution, oldImage.VerticalResolution);
                    Graphics graphics = Graphics.FromImage(newImage);
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.DrawImage(oldImage, new Rectangle(0, 0, Width, Height), 0, 0, oldImage.Width, oldImage.Height, GraphicsUnit.Pixel);
                    this.CheckUpDirectory(base.Server.MapPath(SPath + "/" + package));
                    newImage.Save(base.Server.MapPath(SPath + "/" + package + "/" + FileName + ExtensionName), ImageFormat.Jpeg);
                    newImage.Dispose();
                    oldImage.Dispose();
                    img.Img_Name = package + "/" + FileName + ExtensionName;
                    img.Img_Path = SPath + "/" + package + "/" + FileName + ExtensionName;
                    img.Img_Type = Type;
                    img.Img_Date = DateTime.Now;
                    img = this.imgBll.AddModel(img);
                    File.Delete(base.Server.MapPath("Temp/" + FileName + ExtensionName));
                }
                else
                    img.Img_ID = 0;
                csTemp0000 = img;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError("Dcms.Common.ImageUploadUtil.ImageUpload2", ex);
                throw ex;
            }
            return csTemp0000;
        }

        public Dcms_ImagesModel ImageUpload(string SPath, string BPath, int Width, int Height, string UploadName, int Type)
        {
            Dcms_ImagesModel csTemp0000= null;
            try
            {
                Dcms_ImagesModel img = new Dcms_ImagesModel();
                string package = DateTime.Now.ToString("yyyy-MM-dd");
                this.CheckUpDirectory(BPath + "/" + package);
                HttpPostedFile file = HttpContext.Current.Request.Files[UploadName];
                string ExtensionName = Path.GetExtension(file.FileName);
                if (ExtensionName != string.Empty)
                {
                    string FileName = Guid.NewGuid().ToString();
                    file.SaveAs(BPath + "/" + package + "/" + FileName + "." + ExtensionName);
                    Image oldImage = Image.FromFile(BPath + "/" + package + "/" + FileName + "." + ExtensionName);
                    Bitmap newImage = new Bitmap(Width, Height);
                    newImage.SetResolution(oldImage.HorizontalResolution, oldImage.VerticalResolution);
                    Graphics graphics = Graphics.FromImage(newImage);
                    graphics.SmoothingMode = SmoothingMode.AntiAlias;
                    graphics.DrawImage(oldImage, new Rectangle(0, 0, Width, Height), 0, 0, oldImage.Width, oldImage.Height, GraphicsUnit.Pixel);
                    this.CheckUpDirectory(SPath + "/" + package);
                    newImage.Save(SPath + "/" + package + "/" + FileName + "." + ExtensionName, ImageFormat.Jpeg);
                    newImage.Dispose();
                    oldImage.Dispose();
                    img.Img_Name = package + "/" + FileName + "." + ExtensionName;
                    img.Img_Path = SPath + "/" + package + "/" + FileName + "." + ExtensionName;
                    img.Img_Type = Type;
                    img.Img_Date = DateTime.Now;
                    img = this.imgBll.AddModel(img);
                }
                else
                    img.Img_ID = 0;
                csTemp0000 = img;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError("Dcms.Common.ImageUploadUtil.ImageUpload1", ex);
                throw ex;
            }
            return csTemp0000;
        }

        public bool ThumbnailCallback()
        {
            return false;
        }
    }
}

